//
// math.h - Math library
//




#define Q_COLOR_MASK		((sizeof(colorTable) / sizeof(color_t)) - 1)

extern vec3_t	axisDefault[3];
extern mat4_t	matrixIdentity;

void VectorNormalizeFast (vec3_t v);

int Q_PrintStrlen (const char *string);

void Com_StripExtension (const char *path, char *dst, int dstSize);
void Com_FileExtension (const char *path, char *dst, int dstSize);

#define VectorLerp(a,b,f,o)		((o)[0]=(a)[0]+((b)[0]-(a)[0])*(f),(o)[1]=(a)[1]+((b)[1]-(a)[1])*(f),(o)[2]=(a)[2]+((b)[2]-(a)[2])*(f))

// Plane sides
#define	SIDE_FRONT			0
#define	SIDE_BACK			1
#define	SIDE_ON				2
#define SIDE_CROSS			3

#define sgn(x)					(((x) > 0.0) ? 1.0 : ((x) < 0.0) ? -1.0 : 0.0)

#define SEC2MS(t)				((t) * 1000.0f)

float AngleMod (float angle);
void VectorToAngles (const vec3_t vec, vec3_t angles);
void VectorReflect (const vec3_t v, const vec3_t normal, vec3_t out);
void LerpAngles (const vec3_t from, const vec3_t to, float frac, vec3_t out);

int PointOnPlaneSide2 (const vec3_t point, float epsilon, const cplane_t *plane);

int NearestPowerOfTwo (int number, int roundDown);
bool IsPowerOfTwo (int number);

void VectorMin (const vec3_t v1, const vec3_t v2, vec3_t out);
void VectorMax (const vec3_t v1, const vec3_t v2, vec3_t out);

void SnapVector (vec3_t v);

bool BoundsAndSphereIntersect (const vec3_t mins, const vec3_t maxs, const vec3_t center, float radius);
int BoxOnPlaneSide2 (const vec3_t mins, const vec3_t maxs, const cplane_t *plane);
bool PlaneFromPoints (cplane_t *plane, const vec3_t a, const vec3_t b, const vec3_t c);
int SphereOnPlaneSide2 (const vec3_t center, float radius, const cplane_t *plane);

void SetPlaneSignbits (cplane_t *plane);

void Matrix4_AffineInverse (const mat4_t in, mat4_t out);

void VectorRotate (const vec3_t v, const vec3_t matrix[3], vec3_t out);

bool AxisCompare (const vec3_t axis1[3], const vec3_t axis2[3]);

void Matrix4_Copy (const mat4_t in, mat4_t out);
bool Matrix4_Compare (const mat4_t m1, const mat4_t m2);
void Matrix4_Identity (mat4_t m);
void Matrix4_MultiplyFast (const mat4_t m1, const mat4_t m2, mat4_t out);
void Matrix4_Rotate (mat4_t m, float angle, float x, float y, float z);
void Matrix4_Scale (mat4_t m, float x, float y, float z);
void Matrix4_Translate (mat4_t m, float x, float y, float z);
void Matrix4_Shear (mat4_t m, float x, float y, float z);
void Matrix4_Set (mat4_t m, const vec3_t rotation[3], const vec3_t translation);
void Matrix4_Multiply (const mat4_t m1, const mat4_t m2, mat4_t out);
void Matrix4_Transform (const mat4_t m, const vec4_t in, vec4_t out);
void Matrix4_Transpose (const mat4_t in, mat4_t out);

void MatrixMultiply (const vec3_t in1[3], const vec3_t in2[3], vec3_t out[3]);

void AxisTranspose (const vec3_t in[3], vec3_t out[3]);

void Matrix4_TransformNormal (const mat4_t m, const vec3_t in, vec3_t out);
void Matrix4_TransformVector (const mat4_t m, const vec3_t in, vec3_t out);